package jms;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.winning.bi.data.Message;

/**
 * 消息的生产者（发送者）
 * 
 * @author
 *
 */
public class JMSProducer {

	public static void main(String[] args) {

		// 连接工厂
		ActiveMQConnectionFactory connectionFactory;
		// 连接
		Connection connection = null;
		// 会话 接受或者发送消息的线程
		Session session;
		// 消息的目的地
		Destination destination;
		Destination destination2 ;
		// 消息生产者
		MessageProducer messageProducer;
		// 实例化连接工厂
		connectionFactory = new ActiveMQConnectionFactory("system", "zdc524", "tcp://IP:61616");

		try {
			// 通过连接工厂获取连接
			connection = connectionFactory.createConnection();
			// 启动连接
			connection.start();
			// 创建session
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			// 创建一个名称为HelloWorld的消息队列
			destination = session.createQueue("xxx");
			destination2= session.createQueue("reply");
			// 创建消息生产者
			messageProducer = session.createProducer(destination);
			messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
			// 发送消息
			Message message = new Message();
			// message.get

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("a", 1);
			map.put("b", 2);
			message.setMap(map);
			
			ObjectMessage message1 = session.createObjectMessage();
		    message1.setJMSReplyTo(destination2);
				message1.setObject(message);
				messageProducer.send(message1);

				// 创建消息消费者
				MessageConsumer messageConsumer = session.createConsumer(destination);
				Listener listener = new Listener();
				messageConsumer.setMessageListener(listener);
			
				 while (true) {
		             
		         }


			//session.commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 发送消息
	 * 
	 * @param session
	 * @param messageProducer
	 *            消息生产者
	 * @throws Exception
	 */
	public  void sendMessage(Session session, MessageProducer messageProducer, Serializable serializable)
			throws Exception {

		

	}
}